﻿using EM.FrameWork;
using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.Common;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class AnnouncementController : BaseAuthController
    {
        //
        // GET: /Annoucement/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public JsonResult GetList()
        {
            AnnouncementBLL _announcementBll = new AnnouncementBLL();
            string _kind = Request.QueryString["kind"].ToString();
            List<object> result = new List<object>();
            List<EMOA_Announcement> list = new List<EMOA_Announcement>();
            if (_kind == "AnnounceKind")
                list = _announcementBll.GetList().OrderBy(p => p.CreateTime).ToList();
            else
                list = _announcementBll.GetList(p => p.Kind == _kind).OrderBy(p => p.CreateTime).ToList();
            list.ForEach(p =>
            {
                result.Add(new AnnouncementModels().ToModel<EMOA_Announcement>(p));
            });

            return ToJson(result,list.Count());
        }

        //
        // GET: /Annoucement/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Annoucement/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Annoucement/Create

        [HttpPost]
        public ActionResult Create(AnnouncementModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                    AnnouncementBLL _announcementBll = new AnnouncementBLL();
                    EMOA_Announcement ep = model.ToEntity<EMOA_Announcement>();
                    ep.AnnouncementID = Guid.NewGuid();
                    ep.CreateBy = CurrentUser.UserID;
                    ep.CreateTime = DateTime.Now;

                    if (_announcementBll.Add(ep))
                    {
                        return Content("OK");
                    }
                    else
                    {
                        return Content("失败");
                    }
                //}
                //else
                //{
                //    return Content("验证失败");
                //}
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        //
        // GET: /Annoucement/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Annoucement/Edit/5

        [HttpPost]
        public ActionResult Edit(AnnouncementModels model)
        {
            try
            {
                AnnouncementBLL _announcementBll = new AnnouncementBLL();
                EMOA_Announcement entity = _announcementBll.GetEntity(model.AnnouncementID);
                entity = model.ToEntity<EMOA_Announcement>(entity);
                entity.UpdateBy = CurrentUser.UserID;
                entity.UpdateTime = DateTime.Now;  
                if (_announcementBll.Update(entity))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        //
        // GET: /Annoucement/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Annoucement/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid ID)
        {
            try
            {
                AnnouncementBLL _announcementBll = new AnnouncementBLL();
                if (_announcementBll.Delete(ID))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
    }
}
